import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const app = getApp();
import {
  ajaxGetStoreList,
  ajaxUserRechargeVip
} from '../../../api/ordering'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //自定义导航高度
    customNavHeight: app.globalData.customNavInfo.navHeight,
    baseURL: app.globalData.config.baseURL,
    // tab active
    active: 0,
    // 显示地图
    isShowMap: true,
    // 所有门店
    list: [],
    // 常用门店
    commonShopList: [],
    // 当前选中门店
    currentStoreId: null,
    // 地图配置
    mapConfig: {
      latitude: '',
      longitude: '',
      // 标记点
      markers: []
    },
    prevParams: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.setData({
      prevParams: options || {}
    })
    //loading
    Toast.loading({
      message: '正在加载',
      forbidClick: true,
      duration: 0
    });

    try {
      // 获取位置
      let locationData = await app.getLocation();
      let {
        data
      } = await ajaxGetStoreList({
        longitude: locationData ? locationData.longitude : '',
        latitude: locationData ? locationData.latitude : '',
        ifCommonUse: 'Y'
      })
      let mapConfig = null,
        currentStoreId = null,
        // 所有门店
        allShopList = data.shopList || [],
        // 常用门店
        commonShopList = data.commonUseShopList || [];

      if (app.globalData.shopInfo || (allShopList && allShopList.length)) {
        let mapData = app.globalData.shopInfo || allShopList[0];
        // 设置地图标记点
        mapConfig = {
          latitude: mapData.latitude,
          longitude: mapData.longitude,
          // 标记点
          markers: [{
            id: 1,
            latitude: mapData.latitude,
            longitude: mapData.longitude,
            width: '44rpx',
            height: '56rpx'
          }]
        }
        // 选中门店id
        currentStoreId = mapData.shopId
      }
      this.setData({
        list: allShopList,
        commonUseShopList: commonShopList,
        ...(mapConfig ? {
          mapConfig,
          currentStoreId
        } : {})
      })
      await app.myNextTick()
      Toast.clear();
    } catch (err) {
      await app.myNextTick()
      Toast.clear();
      Toast(err.msg || '错误')
    }
  },
  // 设置显示隐藏地图
  setShowMap() {
    this.setData({
      isShowMap: !this.data.isShowMap
    })
  },
  // 设置选中门店id
  setCheckedStoreId(e) {
    let data = this.data.list.find(item => item.shopId == e.detail)
    if (data) {
      this.setData({
        currentStoreId: data.shopId,
        mapConfig: {
          latitude: data.latitude,
          longitude: data.longitude,
          // 标记点
          markers: [{
            id: 1,
            latitude: data.latitude,
            longitude: data.longitude,
            width: '44rpx',
            height: '56rpx'
          }]
        }
      })
      app.globalData.shopInfo = JSON.parse(JSON.stringify(data))
      // 选择店铺，充值vip创建订单，前往支付下单
      if (this.data.prevParams.userRechargeVipId) {
        this.createVipOrder();
      } else {
        wx.navigateBack({
          delta: 1
        })
      }
    }
  },
  // 充值vip创建订单，前往支付下单
  async createVipOrder() {
    Toast.loading({
      message: '正在处理',
      forbidClick: true,
      duration: 0
    });
    try {
      let {
        data
      } = await ajaxUserRechargeVip({
        "buyDetailUserId": this.data.prevParams.userRechargeVipId,
        "orderSource": app.globalData.shopInfo.shopId,
        "productId": "10032"
      })
      app.storeModules.order.data.triggerRefresh=true;
      app.commonToPage('/pages/ordering/orderPayment/index?orderId=' + data.orderId, 'redirectTo')
      await app.myNextTick()
      Toast.clear();
    } catch (err) {
      await app.myNextTick()
      Toast.clear(err.msg || '错误');
    }
  },
  // tab change
  tabChange(e) {
    this.setData({
      active: e.detail.index || 0
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  }
})